package cn.iocoder.yudao.module.wine.controller.admin.agreement;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.wine.controller.admin.agreement.vo.*;
import cn.iocoder.yudao.module.wine.dal.dataobject.agreement.AgreementDO;
import cn.iocoder.yudao.module.wine.service.agreement.AgreementService;

@Tag(name = "管理后台 - 协议配置")
@RestController
@RequestMapping("/wine/agreement")
@Validated
public class AgreementController {

    @Resource
    private AgreementService agreementService;

    @PostMapping("/create")
    @Operation(summary = "创建协议配置")
    @PreAuthorize("@ss.hasPermission('wine:agreement:create')")
    public CommonResult<Long> createAgreement(@Valid @RequestBody AgreementSaveReqVO createReqVO) {
        return success(agreementService.createAgreement(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新协议配置")
    @PreAuthorize("@ss.hasPermission('wine:agreement:update')")
    public CommonResult<Boolean> updateAgreement(@Valid @RequestBody AgreementSaveReqVO updateReqVO) {
        agreementService.updateAgreement(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除协议配置")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('wine:agreement:delete')")
    public CommonResult<Boolean> deleteAgreement(@RequestParam("id") Long id) {
        agreementService.deleteAgreement(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得协议配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('wine:agreement:query')")
    public CommonResult<AgreementRespVO> getAgreement(@RequestParam("id") Long id) {
        AgreementDO agreement = agreementService.getAgreement(id);
        return success(BeanUtils.toBean(agreement, AgreementRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得协议配置分页")
    @PreAuthorize("@ss.hasPermission('wine:agreement:query')")
    public CommonResult<PageResult<AgreementRespVO>> getAgreementPage(@Valid AgreementPageReqVO pageReqVO) {
        PageResult<AgreementDO> pageResult = agreementService.getAgreementPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AgreementRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出协议配置 Excel")
    @PreAuthorize("@ss.hasPermission('wine:agreement:export')")
    @OperateLog(type = EXPORT)
    public void exportAgreementExcel(@Valid AgreementPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AgreementDO> list = agreementService.getAgreementPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "协议配置.xls", "数据", AgreementRespVO.class,
                        BeanUtils.toBean(list, AgreementRespVO.class));
    }

}